<?php

class ModelFaSupplier extends Model {

    public function addSupplier($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "fa_supplier SET short_name = '" . $this->db->escape($data['short_name']) . "', name = '" . $this->db->escape($data['name']) . "', address = '" . $this->db->escape($data['address']) . "', tax_code = '" . $this->db->escape($data['tax_code']) . "', sort_order = '" . (int) $data['sort_order'] . "'");

        $supplier_id = $this->db->getLastId();

        //add address
        if (isset($data['address_secons'])) {
            foreach ($data['address_secons'] as $address) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_supplier_address SET supplier_id = '" . $supplier_id . "', address = '" . (int) $address['address'] . "', name = '" . $address['name'] . "'");
            }
        }

        //$this->db->query("INSERT INTO " . DB_PREFIX . "fa_address SET address = '". $this->db->escape($data['address']) . "', supplier_id = '" . (int)$supplier_id . "'");

        $this->cache->delete('fa_supplier');
    }

    public function editSupplier($supplier_id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "fa_supplier SET short_name = '" . $this->db->escape($data['short_name']) . "', name = '" . $this->db->escape($data['name']) . "', address = '" . $this->db->escape($data['address']) . "', tax_code = '" . $this->db->escape($data['tax_code']) . "', sort_order = '" . (int) $data['sort_order'] . "' WHERE supplier_id = '" . (int) $supplier_id . "'");

        //add address
        if (isset($data['address_secons'])) {
            $this->db->query("DELETE FROM " . DB_PREFIX . "fa_supplier_address WHERE supplier_id = '" . (int) $supplier_id . "'");
            foreach ($data['address_secons'] as $address) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "fa_supplier_address SET supplier_id = '" . $supplier_id . "', address = '" . $this->db->escape($address['address_supplier']) . "', name = '" . $address['name'] . "'");
            }
        } else {
            $this->db->query("DELETE FROM " . DB_PREFIX . "fa_supplier_address WHERE supplier_id = '" . (int) $supplier_id . "'");
        }

        // $this->db->query("UPDATE " . DB_PREFIX . "address SET address = '". $this->db->escape($data['address']) . "' WHERE supplier_id = '" . (int)$supplier_id . "'");

        $this->cache->delete('fa_supplier');
    }

    public function deleteSupplier($supplier_id) {
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_supplier WHERE supplier_id = '" . (int) $supplier_id . "'");

        //delete address
        $this->db->query("DELETE FROM " . DB_PREFIX . "fa_supplier_address WHERE supplier_id = '" . (int) $supplier_id . "'");
        //$this->db->query("DELETE FROM " . DB_PREFIX . "fa_address WHERE supplier_id = '" . (int)$supplier_id . "'");

        $this->cache->delete('fa_supplier');
    }

    public function getSupplier($supplier_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_supplier WHERE supplier_id = '" . (int) $supplier_id . "'");

        return $query->row;
    }

    public function getSuppliers($data = array()) {
        $sql = "SELECT * FROM " . DB_PREFIX . "fa_supplier";

        if (!empty($data['filter_name'])) {
            $sql .= " WHERE name LIKE '%" . $this->db->escape($data['filter_name']) . "%'";
        }

        if (!empty($data['filter_short_name'])) {
            $sql .= " WHERE short_name LIKE '%" . $this->db->escape($data['filter_short_name']) . "%'";
        }

        $sort_data = array(
            'name',
            'sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int) $data['start'] . "," . (int) $data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    //get address
    public function getAddress($supplier_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "fa_supplier_address WHERE supplier_id = '" . (int) $supplier_id . "'");
        return $query->rows;
    }

    public function getTotalSuppliers() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "fa_supplier");

        return $query->row['total'];
    }

}

?>